<?php


namespace app\manage\controller;

use other\MessageSend;
use think\Db;
use think\Log;

class ClueCost extends Base
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        $acc_name = input('acc_name','');
        $adv_id = input('adv_id','');
        $cityname = input('cityname','');
        $pro_id = input('pro_id','');
        $status = input('status','');


        $map=[];
        if($adv_id){
            $map['account_id']=Db::name('account')->where('adv_id',$adv_id)->value('id');

        }
        if($acc_name != ''){
            $map['account_id']=Db::name('account')->where('acc_name','like','%'.$acc_name.'%')->value('id');
        }

        if($pro_id!=''){
            $map['pro_id']=$pro_id;
        }
        //模糊查询城市
        if(!empty($cityname)){
            $cityIdArr=Db::name('city')->field('id,type,pid')->where('name','like',$cityname."%")->where('type',2)->find();
            if($cityIdArr){
                $map['city']=$cityIdArr['pid'].'-'.$cityIdArr['id'];
            }else{
                $map['city']=0;
            }
        }

        if($status != ''){
            $map['status'] = $status;
        }


        $accountAll=Db::name('account')->column('adv_id,acc_name,channel_id,acc_desc','id');
        $account=model('Account')->getListByMap([],'','',false);

        $productAll =Db::name('product')->column('name','id');
        $products = model('Product')->where('status',1)->column('name','id');

        $infos = model('ClueAddCost')->getInfosByMap($map, '', false);

        foreach ($infos as &$v){
            $accountInfo=$accountAll[$v['account_id']];
            $v['acc_desc']=$accountInfo['acc_desc'];
            $v['acc_name']=$accountInfo['acc_name'];
            $v['adv_id']=$accountInfo['adv_id'];
            $v['pro_name']=$productAll[$v['pro_id']];
            $ids = explode('-',$v['city']);
            $cityNum=count($ids);
            if($cityNum==1){
                $v['city'] = $this->getCityName($ids[0]);
            }elseif ($cityNum==2){
                $prov = $this->getCityName($ids[0]);
                $city = $this->getCityName($ids[1]);
                if($prov == $city){
                    $v['city'] = $prov;
                }else{
                    $v['city'] = $prov.'-'.$city;
                }
            }
        }

        $province = Db::name('city')->where(['pid'=>1])->select();

        return $this->fetch('',[
            'infos' => $infos,

            'province' => $province,
            'status' => $status,
            'account' => $account,
            'products' => $products,
            'pro_id' => $pro_id,
            'status' => $status,
            'cityname' => $cityname,
            'adv_id' => $adv_id,
            'acc_name' => $acc_name,


        ]);
    }



    public function save()
    {
        if(request()->isAjax()){
            try{

                $id = input('post.id');
                $save_data = input('post.');

                if($save_data['province'] != ''){
                    $save_data['city'] = $save_data['province'].'-'.$save_data['city'];
                }else{
                    $save_data['city'] = '';
                }


                if(isset($id) && $id){
                    $save_data['id'] = $id;
                }else{
                    unset($save_data['id']);
                }

                $result = $this->editData(false,'','ClueAddCost',$save_data);
                if($result['code'] == 1001){
                    if(isset($id) && $id){
                        $result['msg'] = '修改成功';
                        $this->RecordSystemLog($this->uid,$this->behavior,'修改成功');
                    }else{
                        $result['msg'] = '保存成功';
                        $this->RecordSystemLog($this->uid,$this->behavior,'保存成功');
                    }
                }

                return $result;
            }catch (\Exception $e){
                Log::error($e->getMessage());
                return self::showReturnCode(1011,$e->getMessage());
            }
        }else{
            return self::showReturnCode(1002);
        }
    }


    

    public function edit()
    {
        $id = input('id');
        if(!$id) return self::showReturnCode(1002,'id 错误');
        $info = model('ClueAddCost')->getInfoByMap(['id'=>$id]);
        if($info['city'] != 0){
            $ids = explode('-',$info['city']);
            $info['prov'] = $ids[0];
            $info['city'] = $ids[1];
            $info['city_name'] = $this->getCityName($ids[1]);
        }else{
            $info['prov'] = 0;
            $info['city'] = 0;
            $info['city_name'] = '';
        }
        return self::showJsonReturnCodeData('1001',$info,'');
    }


    public function status()
    {
        if($this->request->isAjax()){
            $id = input('post.id');
            $res = model('ClueAddCost')->save(['status'=>input('post.status')],['id'=>$id]);
            if($res){
                $this->RecordSystemLog($this->uid,$this->behavior,'状态操作成功,操作ID: '.$id);
                return $this->showReturnCode(1001);
            }else{
                return $this->showReturnCode(1009,'操作失败');
            }
        }else{
            return $this->showReturnCode(1002);
        }
    }



    /**
     * 软删除
     */
    public function delete()
    {
        if($this->request->isAjax()){
            $id = input('post.id');
            $res = model('ClueAddCost')->destroy($id);
            if($res){
                $this->RecordSystemLog($this->uid,$this->behavior,'删除成功,操作ID: '.$id);
                return $this->showReturnCode(1001);
            }else{
                return $this->showReturnCode(1009,'删除失败');
            }
        }else{
            return $this->showReturnCode(1002);
        }
    }
}